set linesize 140
set rmsg on
set scheme s1mono
/***************************************************************************
	Project		:	Army Service in the All-Volunteer Era	
	Author(s)	:	Kyle Greenberg	(kyle.greenberg@westpoint.edu)
					Matthew Gudgeon (matthew.gudgeon@westpoint.edu)
					Adam Isen 		(Adam.Isen@treasury.gov)
					Corbin Miller 	(Corbin.Miller@treasury.gov)
					Rich Patterson 	(rich_patterson@byu.edu)
	File Name	:	het.do
	Description	:	This file runs the main 2SLS analysis on additional
					subsamples
****************************************************************************/

*---- if not running master, set up file structure ----*
*change directory to where programs and subfolders are stored
*cd ""

if "${raw}"==""		global raw		"raw/"
if "${data}"==""	global data		"data/"
if "${output}"==""	global output	"output/"

cap mkdir	"${output}"
cap mkdir	"${output}het/"

*---- select subprograms ----*
local het_regs		1
local het_figs		1

local outcomelist wages anywages anyf1098T
local stratalist black_male black_female white_male white_female A B

*make macros for labels
local anywages_l	"Employed"
local wages_l		"Earnings (Dollars)"
local anyf1098T_l 	"Receives Form 1098-T (College)"

*make macros for display format
foreach x in wages {
	local `x'_f %9.0fc
}
foreach x in anywages anyf1098T {
	local `x'_f %5.3f
}

*---------------------*
*---- regressions ----*
*---------------------*
if `het_regs'==1{
	foreach strata of local stratalist {
		foreach outcome of local outcomelist {
			*---- initialize output file ----*
			clear
			set obs 22
			gen y = _n - 3
			foreach cut in 31 50{
				foreach x in b se N m{
					gen `x'_`strata'_`outcome'_`cut' = .
				}
			}
			save "${output}het/het_results_`strata'_`outcome'", replace
					
			*---- loop over years relative to first AFQT ----*
			if "`strata'"=="B" local last 17
			else local last 19
			forval y=-1/`last' {
				local yt = `y'
				if `y' == -1 local yt m1

				*---- regressions ----*
				use `outcome'_`yt' firstafqt firstafqt_fy firstyear inst31 k31* instk31* inst50 k50* instk50* access quarterFE black white male using "${data}army-treasury-analysis", clear
				
				gen black_male = black==1 & male==1
				gen black_female = black==1 & male==0
				gen white_male = white==1 & male==1
				gen white_female = white==1 & male==0

				gen A = inrange(firstyear,1990,2000)
				gen B = inrange(firstyear,2001,2011)

				keep if `strata'==1

				*1st cutoff (AFQT=31)
				ivregress 2sls `outcome'_`yt' k31 instk31 k31_2 instk31_2 (access=inst31) ib175.quarter if inrange(firstafqt,12,49), robust
				local b_31 = _b[access]
				local se_31 = _se[access]
				local N_31 = e(N)
				sum `outcome'_`yt' if e(sample)
				local m_31 = r(mean)
					
				*2nd cutoff (AFQT=50)
				ivregress 2sls `outcome'_`yt' k50 instk50 k50_2 instk50_2 (access=inst50) ib175.quarter if inrange(firstafqt,31,68), robust
				local b_50 = _b[access]
				local se_50 = _se[access]
				local N_50 = e(N)
				sum `outcome'_`yt' if e(sample)		
				local m_50 = r(mean)

				*---- write estimates to dataset ----*
				use "${output}het/het_results_`strata'_`outcome'", clear
				foreach cut in 31 50{
					foreach x in b se N m{
						replace `x'_`strata'_`outcome'_`cut' = ``x'_`cut'' if y == `y'
					}
				}
				save "${output}het/het_results_`strata'_`outcome'", replace
			}
		}
	}
}	//end if
else di "*---- skipping regressions ----*"

*-----------------*
*---- figures ----*
*-----------------*
if `het_figs'==1{
	foreach strata of local stratalist {
		foreach outcome of local outcomelist {
			*bring in results
			use "${output}het/het_results_`strata'_`outcome'", clear
			keep if inrange(y,-1,19)
					
			*create plotting variables
			gen y1 = y-.1
			gen y2 = y+.2
			foreach cut in 31 50 {
				gen hi_`strata'_`outcome'_`cut' = b_`strata'_`outcome'_`cut' + 1.96*se_`strata'_`outcome'_`cut'
				gen lo_`strata'_`outcome'_`cut' = b_`strata'_`outcome'_`cut' - 1.96*se_`strata'_`outcome'_`cut'
			}
			
			*plot estimates
			tw 	(rcap hi_`strata'_`outcome'_31 lo_`strata'_`outcome'_31 y1, lcolor(black) lp(dash) lw(thin))	///
				(line b_`strata'_`outcome'_31 y1, lcolor(black) lp(dash) lw(medthick))	///
				(rcap hi_`strata'_`outcome'_50 lo_`strata'_`outcome'_50 y2, lcolor(gs7) lw(thin))	///
				(line b_`strata'_`outcome'_50 y2, lcolor(gs7) lw(medthick))	///
				, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
				ytitle("``outcome'_l'") ylabel(,angle(0) format(``outcome'_f'))	///
				xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
				legend(size(small) order (2 4) label(2 "Cutoff = 31") label(4 "Cutoff = 50"))
			graph export "${output}het/het_`strata'_`outcome'.pdf", as(pdf) replace
		}
	}

	*compare male and female by black and white
	local black_male_l "Black Male"
	local black_female_l "Black Female"
	local white_male_l "White Male"
	local white_female_l "White Female"
	foreach outcome of local outcomelist{
		foreach race in black white{
			use "${output}het/het_results_`race'_male_`outcome'", clear
			merge 1:1 y using "${output}het/het_results_`race'_female_`outcome'", nogen
			gen y1 = y-0.1
			gen y2 = y+0.2
			foreach cut in 31 50 {
				gen hi_`race'_male_`outcome'_`cut' = b_`race'_male_`outcome'_`cut' + 1.96*se_`race'_male_`outcome'_`cut'
				gen lo_`race'_male_`outcome'_`cut' = b_`race'_male_`outcome'_`cut' - 1.96*se_`race'_male_`outcome'_`cut'
				gen hi_`race'_female_`outcome'_`cut' = b_`race'_female_`outcome'_`cut' + 1.96*se_`race'_female_`outcome'_`cut'
				gen lo_`race'_female_`outcome'_`cut' = b_`race'_female_`outcome'_`cut' - 1.96*se_`race'_female_`outcome'_`cut'
				tw 	(rcap hi_`race'_male_`outcome'_`cut' lo_`race'_male_`outcome'_`cut' y1, lcolor(gs7) lp(longdash) lw(thin))	///
					(line b_`race'_male_`outcome'_`cut' y1, lcolor(gs7) lp(longdash) lw(medthick))	///
					(rcap hi_`race'_female_`outcome'_`cut' lo_`race'_female_`outcome'_`cut' y2, lcolor(black) lp(solid) lw(thin))	///
					(line b_`race'_female_`outcome'_`cut' y2, lcolor(black) lp(shortdash) lw(medthick))	///
					, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
					ytitle("Earnings (Dollars)") ylabel(,angle(0) format(%6.0fc))	///
					xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
					legend(size(small) order (4 2) label(2 "``race'_male_l' (Cutoff=`cut')") label(4 "``race'_female_l' (Cutoff=`cut')"))
				graph export "${output}het/het_gender_`race'_`outcome'_`cut'.pdf", as(pdf) replace
			}
		}
	}

	*compare application cohorts
	foreach outcome of local outcomelist{
		use "${output}het/het_results_A_`outcome'", clear
		merge 1:1 y using "${output}het/het_results_B_`outcome'", nogen
		gen y1 = y-0.1
		gen y2 = y+0.2
		foreach cut in 31 50 {
			gen hi_A_`outcome'_`cut' = b_A_`outcome'_`cut' + 1.96*se_A_`outcome'_`cut'
			gen lo_A_`outcome'_`cut' = b_A_`outcome'_`cut' - 1.96*se_A_`outcome'_`cut'
			gen hi_B_`outcome'_`cut' = b_B_`outcome'_`cut' + 1.96*se_B_`outcome'_`cut'
			gen lo_B_`outcome'_`cut' = b_B_`outcome'_`cut' - 1.96*se_B_`outcome'_`cut'
			tw 	(rcap hi_A_`outcome'_`cut' lo_A_`outcome'_`cut' y1, lcolor(gs7) lp(longdash) lw(thin))	///
				(line b_A_`outcome'_`cut' y1, lcolor(gs7) lp(longdash) lw(medthick))	///
				(rcap hi_B_`outcome'_`cut' lo_B_`outcome'_`cut' y2, lcolor(black) lp(solid) lw(thin))	///
				(line b_B_`outcome'_`cut' y2, lcolor(black) lp(shortdash) lw(medthick))	///
				, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
				ytitle("``outcome'_l'") ylabel(,angle(0) format(``outcome'_f'))	///
				xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
				legend(size(small) order (4 2) label(2 "Applications in 1990-2000") label(4 "Applications in 2001-2011"))
			graph export "${output}het/het_cohort_`outcome'_`cut'.pdf", as(pdf) replace
		}
	}
}	//end if
else di "*---- skipping figures ----*"
